package com.dyz.wsw.security;
import com.dyz.wsw.common.utils.security.KeyGenerator;
import com.dyz.wsw.common.utils.security.RSA;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

import java.nio.charset.StandardCharsets;
import java.security.*;
import java.util.List;

/**
 * @author dyz
 * Time 2024/6/26 14:59
 */
@SpringBootTest
public class RSATest222 {
    @Value("${wsw.secret.private-key}")
    private String privateKey;

    @Test
    public void doMd5() throws NoSuchAlgorithmException {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        String input = "192.168.31.197"+"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36";
        byte[] digest = md5.digest(input.getBytes(StandardCharsets.UTF_8));
        // 将字节数组转换为16进制字符串
        StringBuilder sb = new StringBuilder();
        //124756ef340daf80196b4124686d651c
        for (byte b : digest) {
            sb.append(String.format("%02x", b & 0xff));
        }
        String s = sb.toString();

        System.out.println(s);
    }


    @Test
    public void RSATest() throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();
        System.out.println("公钥:"+publicKey);
        System.out.println("私钥:"+privateKey);
    }

    @Test
    public void test2() throws Exception {
        KeyPair keyPair = KeyGenerator.generateKeyPair();
        String publicKeyAsString = KeyGenerator.getPublicKeyAsString(keyPair.getPublic());
        String privateKeyAsString = KeyGenerator.getPrivateKeyAsString(keyPair.getPrivate());
        System.out.println(publicKeyAsString);
        System.out.println("----------------");
        System.out.println(privateKeyAsString);
        String message = "lePeGqm9QXpj3M3qPcY7fdSU7PMjwgBNKSA5/1SnuO6jTxbnIeJxR/m7zPCwUJQPUIOUzRSZE4xSgoMenvb5am2JQz7/dTDhT3559IYCEN7L2POd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42RbZRUmyRQdQX+okr9KW5TMdcR7LLMP8QO1g5ndFzxEa8EYJ01X3PFdpgvZP2lRs7b6kskxQKBgQDPz8wrCtKFLJNN2ksIwZMusfH2EPwdmAtQlKom6g/QCD2yHmtIsZgbSZjbZt4Y0AMyK2SP/OgD8pl9pDiZAY9xi77pxhCbX2o03mNs8e71F0bDeSv206+wN4ayRkjx3O12MXiy7UqqJdLQU+4sdAQXxN7mvL/Pxdm2aXyoFHO1OQKBgCDy3t+4uloJEBpbXUEuMc4clXKVoi94vTlz0a6Fhiz1jJgDRaUW4rPBVTThCMae7ICIa76L2W5Spw7LlJ6jldvDuKZZ+O26der9EKNJvcFypHrKqZRVU4lIJwXehUalZyf3bm7KNPmkAxBenPHPMU8PJOaP8N8ROClFrNZapfn5AoGAD3OrAbRPTPmwhkRp+d+y2vD+RcajiGvKw5YaRMR4ooZKWBks7v3NipHhXMb8Uqn84tkZJB+w/MIoKZBxaOcVzGKQsn84suTYV9AFYo9mrjCp4VT8MOEymg625ECXoexEd9pb2CoOuDIRx83F0uZZe4725/823/MdKhQT4ceLhvkCgYEAzQOMc4hAaqNFXRjrJw7/Je49nN/wCy27KdoUg2zfIUls9S+QejBoWkXuwsfvhlH1B0dOeVJWq9OVexHxeVS+LYoxHQmWx2uaV2pGpeMxFN6ASNK2j+Go5fw8OiHnDnmAGExvnGLhnwgj6bJ/afEJUmlUCAPCdVbZv9h2Y6kU1b8=";
        String decrypt = RSA.decrypt(message,se);
        System.out.println(decrypt);
    }
}
